package com.bl.org.controller;

import com.bl.org.service.IEmployeeService;
import com.bl.org.domain.Employee;
import com.bl.org.query.EmployeeQuery;
import com.bl.basic.util.JsonResult;
import com.bl.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
* 后端接口类；
*/
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    public IEmployeeService employeeService;

    /**
     * 接口：添加或修改
     * @param employee  传递的实体
     * @return AjaxResult 响应给前端
     */
    @PutMapping
    public JsonResult addOrUpdate(@RequestBody Employee employee){
        try {
            if( employee.getId()!=null)
                employeeService.update(employee);
            else
                employeeService.save(employee);
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(false,"操作失败");
        }
    }

    /**
    * 接口：删除
    * @param id
    * @return AjaxResult 响应给前端
    */
    @DeleteMapping(value="/{id}")
    public JsonResult delete(@PathVariable("id") Long id){
        try {
            employeeService.delete(id);
            return new JsonResult();
        } catch (Exception e) {
        e.printStackTrace();
            return new JsonResult(false,"删除失败");
        }
    }

    /**
    * 接口：批量删除
    * @param ids
    * @return AjaxResult 响应给前端
    */
    @PatchMapping
    public JsonResult patchDelete(@RequestBody Long[] ids){
        try {
                employeeService.patchDelete(ids);
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(false,"批量删除失败");
        }
    }

    /**
    * 接口：查询单个对象
    * @param id
    */
    @GetMapping("/{id}")
    public Employee findOne(@PathVariable("id")Long id) {
        return employeeService.loadById(id);
    }


    /**
    * 接口：查询所有
    * @return
    */
    @GetMapping
    public List<Employee> findAll(){
        return employeeService.loadAll();
    }


   /**
    * 接口：分页查询或高级查询
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping
    public PageList<Employee> queryPage(@RequestBody EmployeeQuery query) {
        return employeeService.queryPage(query);
    }

    /**
     * 接口：接单查询收购人
     * @return
     */
    @GetMapping("/list/{shopId}")
    public List<Employee> loadEmpByShopId(@PathVariable("shopId") Long shopId){
        return employeeService.loadEmpByShopId(shopId);
    }

}
